import os
import csv
from xml.etree.ElementTree import parse

###################################
##### defining class "writer" #####
###################################

numbers = open('D:\\numbers.csv', 'w', encoding='utf-8', newline='')
wrnumbers = csv.writer(numbers)
wrnumbers.writerow(['appnum', 'registernum', 'opennum', 'publicnum'])


class writer:

    def __init__(self, x):
        self.appnumber = str(x[:-4])
        self.year = self.appnumber[2:6]
        self.xmldir = 'D:\\Data\\KIPRIS\\Biblio\\'+str(self.year) + '\\' + str(self.appnumber) + ".xml"
        try :
            tree = parse(self.xmldir)
            self.parse_error = 0
        except :
            self.parse_error = 1
        if self.parse_error ==0:
            tree = parse(self.xmldir)
            self.note = tree.getroot()
            self.successYN = self.note.find("header").find("successYN").text
            if self.successYN != 'N':
                self.normal = self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo")
            else :
                self.normal = None
        else :
            self.normal = None
    
    def numbers(self):
        if self.normal == None:
            pass
        else :
            if self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("registerNumber") != None:
                registernum = self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("registerNumber").text
            else :
                registernum = ''
            if self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("openNumber") != None:
                opennum = self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("openNumber").text
            else :
                opennum = ''
            if self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("publicationNumber") != None:
                publicnum = self.note.find("body").find("item").find("biblioSummaryInfoArray").find("biblioSummaryInfo").find("publicationNumber").text
            else :
                publicnum = ''
            row = [str(self.appnumber), str(registernum), str(opennum), str(publicnum)]
            wrnumbers.writerow(row)

################################################################################################################################
################################################################################################################################
################################################################################################################################


for year in range(1948, 2017):

    xmllist = os.listdir('D:\\Data\\KIPRIS\\Biblio\\'+str(year))
    errorlist = []
    for files in xmllist:
        a = writer(files)
        if a.parse_error ==1 or a.successYN == 'N':
            errorlist.append(a.appnumber)
        else:
            pass
        a.numbers()
        print(str(a.appnumber))

    ############################
    ##### Saving errorlist #####
    ############################

    with open("D:\\errorlist_"+ str(year)+".csv", "w", newline = '') as errorlist_csv:
        wr = csv.writer(errorlist_csv)
        wr.writerow(errorlist)